package com.example.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import android.widget.Toast;

import com.pablosz.worm.LifecicleComponent;
import com.pablosz.worm.WormGame;
import com.pablosz.worm.components.Fruit;
import com.pablosz.worm.components.ProcessControlListener;
import com.pablosz.worm.components.Score;
import com.pablosz.worm.components.Worm;
import com.pablosz.worm.util.Hashtable;

public class MainActivity extends Activity implements SurfaceHolder.Callback,
		ProcessControlListener {

	static final String TAG = MainActivity.class.getName();

	SurfaceView sfv;

	public boolean onTouchEvent(MotionEvent event) {
		Log.d(TAG, "touhc");

		int eventaction = event.getAction();

		switch (eventaction) {
		case MotionEvent.ACTION_DOWN:
			break;

		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
			((ApplicationDemo) getApplication()).getGame().notifyOnTouch(
					event.getX(), event.getY());
			Log.d(TAG, "touhc Up");
			break;
		}
		return true;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		sfv = (SurfaceView) findViewById(R.id.surfaceview);
		sfv.getHolder().addCallback(this);

		((ApplicationDemo) getApplication()).getGame()
				.setProcessControlListener(this);
		Log.d(TAG, "Se creo surfaceView");

	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		Log.d(TAG, "Change surfaceView");

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Log.d(TAG, "Create surfaceView");
		((ApplicationDemo) getApplication()).getGame().startLifecicle(holder);
		Toast.makeText(this,
				((ApplicationDemo) getApplication()).getUserName(),
				Toast.LENGTH_LONG);
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Log.d(TAG, "destroyed surfaceView");

	}

	@Override
	public void onFruitEaten(Hashtable<LifecicleComponent> components) {
		Log.d(TAG, "onFruitEaten");

	}

	@Override
	public void onGameOver(Hashtable<LifecicleComponent> components) {
		Log.d(TAG, "onGameOver");

		Double hightSocre = ((ApplicationDemo) getApplication()).getHigtScore();
		if (hightSocre.doubleValue() < ((Score) components
				.get(WormGame.COMPONENT_SCORE)).getPoints())
			((ApplicationDemo) getApplication())
					.setHigtScore(((Score) components
							.get(WormGame.COMPONENT_SCORE)).getPoints());

		Intent i = new Intent(this, EndGameActivity.class);
		i.putExtra("score",
				((Score) components.get(WormGame.COMPONENT_SCORE)).getPoints());
		i.putExtra("size", ((Worm) components.get(WormGame.COMPONENT_WORM))
				.getCurrentSize());
		i.putExtra("count", ((Fruit) components.get(WormGame.COMPONENT_FRUIT))
				.getFruitCount());
		startActivity(i);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);

		return true;
	}

	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_config:
			startActivity(new Intent(this, SettingsActivity.class));
			break;
		case R.id.action_new_game:
			((ApplicationDemo) getApplication()).getGame().resetGame();
			break;
		}	
		return true;
	}

}
